package com.example.tms.controller;

import com.example.tms.dto.ColleagueDTO;
import com.example.tms.entity.EmployeeNode;
import com.example.tms.repository.neo4j.EmployeeGraphRepository;
import com.example.tms.service.GraphSyncService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/relations")
public class RelationController {

    private final EmployeeGraphRepository repository;
    private final GraphSyncService syncService;

    public RelationController(EmployeeGraphRepository repository,
                              GraphSyncService syncService) {
        this.repository = repository;
        this.syncService = syncService;
    }
    // 手动触发同步
    @PostMapping("/sync")
    public void triggerSync() {
        syncService.fullSync();
    }

    // 1. 查询管理链条
    @GetMapping("/managementchain/{employeeId}")
    public List<List<EmployeeNode>> getManagementChain(
            @PathVariable String employeeId) {
        return repository.findManagementChain(employeeId);
    }

    // 2. 查询协作同事（带共同项目）
    @GetMapping("/collaborators/{employeeId}")
    public List<com.example.tms.dto.CollaboratorDTO> getCollaborators(
            @PathVariable String employeeId) {
        return repository.findCollaborators(employeeId);
    }

    // 3. 查询同事关系
    @GetMapping("/colleagues/{employeeId}")
    public List<ColleagueDTO> getColleagues(
            @PathVariable String employeeId) {
        return repository.findColleagues(employeeId);
    }

    // 综合关系网络查询
    @GetMapping("/network/{employeeId}")
    public Map<String, Object> getRelationshipNetwork(@PathVariable String employeeId) {
        Map<String, Object> result = new HashMap<>();
        result.put("management", repository.findManagementChain(employeeId));
        result.put("collaborators", repository.findCollaborators(employeeId));
        result.put("colleagues", repository.findColleagues(employeeId));
        result.put("employee", syncService.createBasicEmployeeNode(employeeId));
        return result;
    }

}
